<template>
  <div>
    <el-form-item label="显示全选">
      <el-switch v-model="localData.all" />
    </el-form-item>
    <el-form-item label="显示边框" v-if="!localData.button">
      <el-switch v-model="localData.border" />
    </el-form-item>
    <el-form-item label="按钮模式" v-if="!localData.border">
      <el-switch v-model="localData.button" />
    </el-form-item>
    <el-form-item label="尺寸" v-if="localData.border || localData.button">
      <el-radio-group v-model="localData.size" size="default">
        <el-radio-button label="large">正常</el-radio-button>
        <el-radio-button label="default">小</el-radio-button>
        <el-radio-button label="small">超小</el-radio-button>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="数据配置">
      <span style="margin-right: 8px">{{ getDictType }}</span>
      <text-edit-button text="配置" @click="openDictEdit" />
    </el-form-item>
    <el-form-item label="数据过滤函数">
      <el-select v-model="localData.filterFunc" filterable clearable>
        <el-option v-for="opt in getPageFuncs()" :key="opt.funcName" :label="opt.funcLabel" :value="opt.funcName" />
      </el-select>
    </el-form-item>
    <el-form-item label="标题绑定属性">
      <el-cascader :props="CascaderProps" :options="attrList" v-model="localData.bindLabel" placeholder="请选择属性" filterable clearable :show-all-levels="false">
        <template #default="{ data: opt }">
          <span>{{ `${opt.entityDesc}(${opt.entityName})` }}</span>
        </template>
      </el-cascader>
    </el-form-item>
    <el-form-item label="选项配置">
      <div v-for="(value, key) in localData.props" :key="key">
        <span>{{ key }}</span>
        <el-input size="default" v-model="localData.props[key]" clearable placeholder="key配置" />
      </div>
    </el-form-item>

    <dict-data-setting v-model="visible" :data="localData" :options="formJson" @submit="handleDictUpdate" />
  </div>
</template>

<script>
import dictDataSetting from '../../components/dictDataSetting.vue';
export default {
  name: 'ConfigCheckbox',
  components: { dictDataSetting },
  props: {
    data: {
      type: Object,
      default: () => {}
    },
    attrList: {
      type: Array,
      default: () => []
    },
    formJson: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      localData: {},
      visible: false,
      CascaderProps: {
        value: 'entityName',
        label: 'entityDescName',
        children: 'childList',
        emitPath: false
      }
    };
  },
  computed: {
    getDictType() {
      switch (this.localData.dicOption) {
        case 'static':
          return '静态数据';
        case 'dictionary':
          return `数据字典(${this.localData.dictionary})`;
        case 'remote':
          return '接口获取';
        default:
          return '静态数据';
      }
    }
  },
  watch: {
    data: {
      handler(newVal) {
        this.localData = newVal;
        this.initData();
      },
      deep: true,
      immediate: true
    },
    localData: {
      handler(newVal) {
        this.$emit('update:data', newVal);
      },
      deep: true
    }
  },
  created() {},
  methods: {
    // 老数据兼容处理
    initData() {
      if (this.localData && !this.localData.params) {
        this.localData.params = {
          withFormData: true
        };
      }
    },
    openDictEdit() {
      this.visible = true;
    },
    handleDictUpdate(data) {
      for (let key in data) {
        this.localData[key] = data[key];
      }
      // this.localData = data;
    },
    getPageFuncs() {
      return this.formJson.funcs ?? [];
    }
  }
};
</script>

<style lang="scss" scoped></style>
